﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win.Model;

namespace Win.DAL
{
    public class TableDAL
    {
        DbContext dbContext = new exp03dbEntities1();

        public List<TableInfo> GetTablesList()
        {
            DbSet<TableInfo> tables = dbContext.Set<TableInfo>();
            List<TableInfo> tableInfos = new List<TableInfo>(tables);
            return tableInfos;
        }

        public TableInfo GetTableById(int id)
        {
            return dbContext.Set<TableInfo>().Where(u => u.TId.Equals(id)).FirstOrDefault();
        }

        public List<TableInfo> GetTableBYNT(string name,string type)
        {
            if (name.Equals("") && type.Equals("全部"))
            {
                DbSet<TableInfo> tables = dbContext.Set<TableInfo>();
                List<TableInfo> tableInfos = new List<TableInfo>(tables);
                return tableInfos;
            }
            else 
            {
                if(!name.Equals(""))
                {
                    List<TableInfo> tableInfos = dbContext.Set<TableInfo>().Where(u =>u.TTitle.Equals(name)).ToList<TableInfo>();
                    return tableInfos;
                }
                else
                {
                    List<TableInfo> tableInfos = dbContext.Set<TableInfo>().Where(u => u.TIsFree.Equals(type)).ToList<TableInfo>();
                    return tableInfos;
                }
            }
        }

        public void AddTable(string name,string type)
        {
            TableInfo table = new TableInfo(name, type);
            dbContext.Entry<TableInfo>(table).State = EntityState.Added;
            dbContext.SaveChanges();
        }

        public void DelTable(int id)
        {
            TableInfo table = GetTableById(id);
            dbContext.Entry<TableInfo>(table).State = EntityState.Deleted;
            dbContext.SaveChanges();
        }
    }
}
